import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/papers_model.dart';
import 'package:appflutterhomeschool/model/tab_model.dart';
import 'package:appflutterhomeschool/view_model/my_app_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

///用户： ninetailedfox
///时间： 2020/7/5
///邮箱： 1037438704@qq.com
///功能： 考试分析报告状态管理

class AnalysisReportViewModel extends ChangeNotifier {
  BuildContext me;
  bool openScreen = false;

  List<PapersModel> listPaperModel = [];

  List<TabModel> listTab = [
    TabModel("数学", true),
    TabModel("语文", false),
    TabModel("英语", false),
    TabModel("化学", false),
  ];

  AnalysisReportViewModel(this.me);

  initView() {
    MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(me);

    if (myAppViewModel.identity) {
      DioUtils.getInstance().post(ApiService.getClsReports, {
        "token": Constant.getToken(),
        "subject": myAppViewModel.studentInformationModel.subjectId,
        "cls_id": myAppViewModel.studentInformationModel.classId,
      }, (successCallBack) {
        if (successCallBack["data"] == null) {
          return;
        }
        if (successCallBack["data"]["papers"] == null) {
          return;
        }
        listPaperModel.clear();
        (successCallBack["data"]["papers"] as List).forEach((element) {
          listPaperModel.add(PapersModel.fromJson(element));
        });
        notifyListeners();
      }, (errorCallBack) {});
    } else {
      Map<String, dynamic> map = {
        "token": Constant.getToken(),
//        "subject_id": myAppViewModel.studentInformationModel.studentNo,
        "cls_id": myAppViewModel.studentInformationModel.classId,
        "student_id": myAppViewModel.studentInformationModel.subjectId,
      };
      DioUtils.getInstance().post(ApiService.getStudentReport, map,
          (successCallBack) {
        if (successCallBack["data"] == null) {
          return;
        }
        if (successCallBack["data"]["reports"] == null) {
          return;
        }
        listPaperModel.clear();
        (successCallBack["data"]["reports"] as List).forEach((element) {
          listPaperModel.add(PapersModel.fromJson(element));
        });
        notifyListeners();
      }, (errorCallBack) {});
    }
  }

//是否开启筛选
  onTapOpenScreen() {
    openScreen = !openScreen;
    notifyListeners();
  }

  tapModelOnTap(int index) {
    for (int i = 0; i < listTab.length; i++) {
      listTab[i].display = false;
    }
    listTab[index].display = true;
    notifyListeners();
  }
}
